// pages/auth/auth.js
const util = require('../../../utils/util.js')
const app = getApp();
var hopeApi = require('../../../vendor/hope-sdk/core/request.js');
var utils = require('../../../vendor/hope-sdk/core/utils.js');
var config = require('../../../config.js');
var srsdkApi = require('../../../vendor/hope-sdk/index.js');
const QQMapWX = require('../../../libs/qqmap-wx-jssdk.min.js');
let qqmapsdk;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    shop:null,
    disabled: false,
    type: "primary",
    amapPlugin: null,
    covers: []
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    let obj = wx.getMenuButtonBoundingClientRect();
    this.setData({
      width: obj.left,
      height: obj.top + obj.height + 8,
      inputTop: obj.top + (obj.height - 30) / 2,
      id : options.id || 0
    }, () => {
      wx.getSystemInfo({
        success: (res) => {
          that.setData({
            //略小，避免误差带来的影响
            dropScreenH: that.data.height * 750 / res.windowWidth - 65
          })
        }
      });
      that.init();
    });
  },
  init:function(){
    var that = this;
    hopeApi.request(app, {
      url: config.service.shopInfo,
      login: true,
      data:{
        id:that.data.id
      },
      success: function (res) {
        let covers = [];
        covers.push({
          id: 1,
          latitude: res.data.data.latitude,
          longitude: res.data.data.longitude,
          title: res.data.data.nickname
        })
        that.setData({
          shop: res.data.data,
          covers: covers
        });
      },
      authfail: function (e) {
        wx.redirectTo({
          url: "../../auth/auth"
        })
      }
    });
  },
  marker: function (e) {
    const that = this
    const shop = that.data.shop;
    const menu = shop.mobile ? ["打电话", "到这里"] : ["到这里"];
    wx.showActionSheet({
      itemList: menu,
      success(res) {
        if (res.tapIndex == 0 && shop.mobile) {
          wx.makePhoneCall({
            phoneNumber: shop.mobile
          })
        } else {
          const latitude = Number(shop.latitude)
          const longitude = Number(shop.longitude)
          wx.openLocation({
            latitude,
            longitude
          })
        }
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  goto: function () {
    const that = this
    const shop = that.data.shop;
    const latitude  = Number(shop.latitude)
    const longitude = Number(shop.longitude)
    wx.openLocation({
      latitude,
      longitude
    });
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})